package com.ebt.m.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.commons.widgets.view.EBTDialog;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;
import com.ebt.m.utils.ConfigData;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
 * Created by wanglu on 2017/8/21.
 */

public class AuthenticationActivity extends BaseToolBarActivity {
    @BindView(R.id.action_authentication)
    Button actionAuthentication;
    private EBTProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authentication);
        ButterKnife.bind(this);
        setTitle(getResources().getString(R.string.app_name));
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
        initProgress();
    }

    private void initProgress() {
        progressDialog = new EBTProgressDialog(this);
        progressDialog.setCanceledOnTouchOutside(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_authentication, menu);
        return super.onCreateOptionsMenu(menu);

    }

    @OnClick(R.id.action_authentication)
    public void onViewClicked() {
        String authUrl = ConfigData.AUTH_URL + AppContext.getCurrentUser().getUserName() + "&fromApp=true";
        Logger.e("authUrl:" + authUrl);
        gotoAuth(authUrl);
    }

    private void gotoAuth(String moduleUrl) {
        Intent webIntent = new Intent(this, WebBaseActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, "去认证");
        bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, false);
        bundle.putString(WebBaseActivity.WEB_TYPE_LINK, moduleUrl);
        bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, WebBaseActivity.FRAGMENT_4AUTHENTICATION);
        webIntent.putExtras(bundle);
        startActivity(webIntent);
    }

    private void showProgress(boolean show) {
        if (show) {
            progressDialog.show();
        } else {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        EBTDialog mLoginOutConfirmDialog = new EBTDialog();
        mLoginOutConfirmDialog.setMessage("确定要退出登录吗？");
        mLoginOutConfirmDialog.setRightButton("退出", v -> AppContext.loginOut(this));
        mLoginOutConfirmDialog.setLeftButton("取消", v -> mLoginOutConfirmDialog.dismiss());
        mLoginOutConfirmDialog.show(getSupportFragmentManager(), getClass().getSimpleName());

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
